﻿using MediatR;
using WarehouseManagement.Application.Command.AllocateCommand;
using WarehouseManagement.Domain.DTO.Allocate;
using WarehouseManagement.Domain.DTO.RequestTransfer;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.AllocateCommandHandler
{
    /// <summary>
    /// 仓库调拨
    /// </summary>
    public class AllocateFanCommandHandler : IRequestHandler<AllocateFanCommand, UpdAllocateDto>
    {
        private readonly IAllocateRepository _allocateRep;
        private readonly IAllocationProductsRepository _allocationProductsRep;

        public AllocateFanCommandHandler(IAllocateRepository allocateRep, IAllocationProductsRepository allocationProductsRep)
        {
            this._allocateRep = allocateRep;
            this._allocationProductsRep = allocationProductsRep;
        }

        public async Task<UpdAllocateDto> Handle(AllocateFanCommand request, CancellationToken cancellationToken)
        {
            var list = (from a in await _allocateRep.GetAll() select a).Where(t => t.AllocateId == request.AllocateId).ToList();
            var item = (from b in await _allocationProductsRep.GetAll() select b).ToList();
            var result = (from c in list
                          select new UpdAllocateDto
                          {
                              AllocateId = c.AllocateId,
                              AName = c.AName,
                              ANumber = c.ANumber,
                              ADate = c.ADate,
                              AWarehousePersonnel = c.AWarehousePersonnel,
                              AGoWarehouse = c.AGoWarehouse,
                              WarehouseId = c.WarehouseId,
                              ARecallProduct = c.ARecallProduct,
                              ProductId = c.ProductId,
                              AState = c.AState,
                              UpdName = c.UpdName,
                              UpdTime = c.UpdTime,
                              Office_Id = c.Office_Id,
                              User_Id = c.User_Id,
                              upddto = (from d in item
                                        where d.ANumber == c.ANumber
                                        select new FanAllocationProducts
                                        {
                                            AllocationProductsId = d.AllocationProductsId,
                                            ANumber = d.ANumber,
                                            APAllocateNum = d.APAllocateNum,
                                            APInboundWarehouse = d.APInboundWarehouse,
                                            PNumber = d.PNumber,
                                            PName = d.PName,
                                            PSpec = d.PSpec,
                                            PUnit = d.PUnit,
                                            WName = d.WName,
                                            SPConfirmNum = d.SPConfirmNum,
                                            AddName = d.AddName,
                                            AddTime = d.AddTime,
                                            UpdName = d.UpdName,
                                            UpdTime = d.UpdTime,
                                            Office_Id = d.Office_Id,
                                            User_Id = d.User_Id,
                                        }).ToList()
                          });
            return result.FirstOrDefault();
        }
    }
}
